<?php
//$Id: view.php 397 2012-05-27 08:20:57Z tomqin@gmail.com $

/**
 * View 视图类
 *
 * @copyright       Copyright (C) 2010-2012 ARESCMS Inc.
 * @author          TomQin <tomqin@gmail.com>
 * @license         http://www.arescms.cn/license/
 * @package Core
 */

class View{

    private static $data = array();

    private static $tpls = array();

    /**
     * 设置一个值
     *
     * @param string
     * @param mixed
     * @return void
     */
    public static function setVar($key, $var = ''){
        self::$data[$key] = $var;
    }

    /**
     * 返回一个值
     *
     * @param string
     * @return mixed
     */
    public static function getVar($key){
        return isset(self::$data[$key]) ? self::$data[$key] : null;
    }

    public static function addView($tplname){
        self::$tpls[] = trim($tplname);
    }

    public static function display(){
        extract(self::$data);
        if(is_array(self::$tpls) && count(self::$tpls)){
            foreach(self::$tpls as $tpl){
                include_once self::getTpl($tpl);
            }
        }
    }

    public static function getTpl($tplname){
        return App::getConfig('view.template_dir').$tplname.'.tpl.php';
    }

    public static function dump(){
        var_dump(self::$data);
    }

    public static function options(array $array, $selected){
        $ret = '';
        foreach($array as $key => $var){
            $sel = ($key == $selected) ? ' selected' : '';
            $ret .= '<option value="'.h($key).'"'.$sel.'>'.h($var).'</option>';
            $ret .= "\n";
        }
        return $ret;
    }

    public static function groupOptions(array $array, $selected){
        $ret = '';
        if(!is_array(current($array))){
            $array = array($array);
        }
        foreach($array as $grp => $arr){
            $ret .= '<optgroup label="'.h($grp).'">';
            $ret .= self::options($arr, $selected);
            $ret .= '</optgroup>';
        }
        return $ret;
    }
}
?>